package org.example.struct.array2;

/**
 * @author liyishan
 * @date 2023/10/17 09:37
 * @apiNote
 */

public class Test00775IsIdealPermutation {

    public static void main(String[] args) {
        int[] nums = {1,0,2};
        System.out.println(test2(nums));
    }

    public static boolean test3(int[] nums) {
        for(int i = 0; i < nums.length; i++){
            if(Math.abs(nums[i] - i) > 1){
                return false;
            }
        }
        return true;
    }

    public static boolean test2(int[] nums) {
        int n = nums.length, minSuff = nums[n - 1];
        for (int i = n - 3; i >= 0; i--) {
            if (nums[i] > minSuff) {
                return false;
            }
            minSuff = Math.min(minSuff, nums[i + 1]);
        }
        return true;
    }


    /**
     * 超时
     * @param nums
     * @return
     */
    public static boolean test1(int[] nums) {
        int n = nums.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 2; j < n; j++) {
                if (nums[i] > nums[j]) {
                    return false;
                }
            }
        }
        return true;
    }

    public static boolean test(int[] nums) {
        int count1 = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            for (int j = i + 1; j < nums.length; j++) {
                if (nums[i] > nums[j]) {
                    count1++;
                }
            }
        }
        int count2 = 0;
        for (int i = 0; i < nums.length - 1; i++) {
            if (nums[i] > nums[i + 1]) {
                count2++;
            }
        }
        return count1 == count2;
    }
}
